#pragma once

#if C2504
#pragma message("Texture.h")
#endif

// Local - has gd3dDevice(ID3D11Device) and other global declarations
#include "Resources.h"

class TextureMgr;

/* ==========================================================================
 * Texture or Surface : This objects of this class can operate in 2 modes !!
 * ==========================================================================
 * Texture		texture for rendering. Can have mipmaps and can be compressed
 * 	            (depends on file format). Not GDI-compatible can't be used in
 * 	            D3D11Pad or Blit/Fill functions. Gets immediately converted if
 * 	            such request has been made.
 * Surface		texture for GDI, D3D11Pad and Blit/Fill usage. Can't have  mipmaps
 *    			Can't be compressed. Can only have B8G8R8A8 format.
 */
class Texture {
	friend class Overlay;
	friend class TextureMgr;
public:	
	Texture( TextureMgr * textureMgr, DWORD w, DWORD h );
	Texture( TextureMgr * textureMgr, DWORD w, DWORD h, void *data );
	Texture( TextureMgr * textureMgr, DWORD w, DWORD h, DXGI_FORMAT Format, const char *fname, bool alpha );
	Texture( TextureMgr * textureMgr, const char *fname, const char *nameWithPath = nullptr );
	~Texture();

	bool HasAlpha()	{ return has_alpha; }
	bool IsTex()	{ return bTex;	}
	void GetSize( DWORD *w, DWORD *h );
	void SetColorKey( DWORD ckey );
	char *GetFileName()	{	return file_name;	}
	HDC GetDC();
	void ReleaseDC();

	ID3D11Texture2D *GetTex()			{	return Tex;		}
	IDXGISurface1 *GetSurf()			{	return Surf;	}
	ID3D11ShaderResourceView **GetSRV()	{	return bSRV ? &SRV : CreateSRV();	}
	ID3D11RenderTargetView **GetRTV()	{	return bRTV ? &RTV : CreateRTV();	}
	bool IsGDIUsed() const { return m_gdiUsed; }
private:
	TextureMgr * m_textureMgr;
	bool FindAlpha( DXGI_FORMAT format );
	ID3D11ShaderResourceView **CreateSRV();
	ID3D11RenderTargetView **CreateRTV();

	CComPtrEx<IDXGISurface1> Surf;	//used only for surfaces
	CComPtrEx<ID3D11Texture2D> Tex;
	CComPtrEx<ID3D11Texture2D> m_gdiTex;
	CComPtrEx<ID3D11ShaderResourceView> m_gdiTexSRV;
	CComPtrEx<ID3D11RenderTargetView> m_gdiTexRTV;

	bool CreateGDIShadowSurface();

	UINT RefCounter;
	DWORD
		width,
		height;
	D3DXCOLOR ColorKey;
	bool
		bTex,		//is texture
		bColorKey,	//has assigned color key
		bSRV,	//SRV was created
		bRTV;	//RTV was created
	bool m_gdiUsed;
	CComPtrEx<ID3D11ShaderResourceView> SRV;
	CComPtrEx<ID3D11RenderTargetView> RTV;

	char *file_name;
	bool has_alpha;
};
